<?php

use think\migration\Migrator;

class UserInviteLog extends Migrator
{
    /**
     * Change Method.
     *
     * Write your reversible migrations using this method.
     *
     * More information on writing migrations is available here:
     * http://docs.phinx.org/en/latest/migrations.html#the-abstractmigration-class
     *
     * The following commands can be used in this method and Phinx will
     * automatically reverse them when rolling back:
     *
     *    createTable
     *    renameTable
     *    addColumn
     *    renameColumn
     *    addIndex
     *    addForeignKey
     *
     * Remember to call "create()" or "update()" and NOT "save()" when working
     * with the Table class.
     */
    protected $name = 'user_invite_log';

    public function up()
    {
        $table = $this->table($this->name, [
            'engine'      => 'InnoDB',
            'collation'   => 'utf8mb4_general_ci',
            'id'          => 'id',
            'signed'      => false,
            'primary_key' => ['id'],
            'comment'     => '用户邀请记录'
        ]);
        $table->addColumn('inviter_id', 'integer', [
            'limit' => 11, 'signed' => false, 'null' => false, 'comment' => '邀请人',
        ])->addColumn('invitee_id', 'integer', [
            'limit' => 11, 'signed' => false, 'null' => false, 'comment' => '被邀请人',
        ])->addColumn('award_score', 'integer', [
            'limit' => 11, 'signed' => false, 'null' => false, 'default' => 0, 'comment' => '奖励积分',
        ])->addColumn('create_at', 'datetime', [
            'null' => false, 'comment' => '邀请时间',
        ])->addIndex(['inviter_id', 'create_at'], [
            'name' => 'inviter_id'
        ])->save();
    }

    public function down(): void
    {
        $table = $this->table($this->name);
        $table->exists() && $table->drop()->save();
    }
}
